package com.gjw.planeWar;

import com.gjw.strategy.FireStrategy;
import com.gjw.strategy.OneFireStrategy;
import com.gjw.strategy.ThreeFireStrategy;
import com.gjw.strategy.TwoFireStrategy;
import com.gjw.style.Style;
import lombok.Data;

import java.awt.*;
import java.util.Objects;
import java.util.Random;

/**
 * @author gjw19
 * @date 2019/5/2
 */
@Data
public class HeroPlane extends GameObject {
    // 英雄飞机坐标
    private int x;
    private int y;
    // 英雄飞机方向
    private HeroDir herodir = HeroDir.STOP;

    // 英雄飞机速度
    private static final int SPEED = 10;
    // 英雄飞机大小
    private static Style style = Style.getINSTANCE();
    public static int WIDTH = style.heroPlane.getWidth();
    public static int HEIGHT = style.heroPlane.getHeight();

    public int hp = 1;

    private boolean living = true;
    Rectangle rect = new Rectangle();
    GameModel gm = GameModel.getInstance();
    FireStrategy fs;
    private static final Props props = Props.getInstance();

    public HeroPlane(int x, int y, HeroDir herodir) {
        this.x = x;
        this.y = y;
        this.herodir = herodir;
        this.gm = gm;

        rect.x = this.x;
        rect.y = this.y;
        rect.width = WIDTH;
        rect.height = HEIGHT;

        gm.add(this);
    }

    @Override
    public void paint(Graphics g) {
        addHP();
        Color color = g.getColor();
        g.setColor(Color.RED);
        for (int i = 0; i < this.getHp(); i++) {
            g.fillRect(50 + i * 12, 96, 10, 15);
        }
        g.setColor(color);
        if (!this.isLiving()) {
            return;
        }
        Random random = new Random();
        g.drawImage(style.heroPlane, x, y, null);
        move();
        if (random.nextInt(100) > 88) {
            fire();
        }
    }

    // 移动方法
    private void move() {

//        未移动直接返回
        if (Objects.equals(herodir, HeroDir.STOP)) {
            return;
        }
        // 根据按下的按钮进行移动
        switch (herodir) {
            case LEFT:
                x -= SPEED;
                break;
            case UP:
                y -= SPEED;
                break;
            case RIGHT:
                x += SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
        }
        boundsCheck();

        rect.x = this.x;
        rect.y = this.y;
    }

    // 英雄飞机边界检测
    private void boundsCheck() {

        if (x < -HeroPlane.WIDTH / 2) {
            x = -HeroPlane.WIDTH / 2;
        }
        if (x > props.GAME_WIDTH - HeroPlane.WIDTH / 2) {
            x = props.GAME_WIDTH - HeroPlane.WIDTH / 2;
        }
        if (y < 0) {
            y = 0;
        }
        if (y > props.GAME_HEIGHT - HeroPlane.HEIGHT / 2) {
            y = props.GAME_HEIGHT - HeroPlane.HEIGHT / 2;
        }
    }

    // 发射子弹
    public void fire() {
        if (props.SHOOT_ENEMY_NUM >= 0
                && props.SHOOT_ENEMY_NUM < props.TWO_FIRE) {
            fs = new OneFireStrategy();
        } else if (props.SHOOT_ENEMY_NUM >= props.TWO_FIRE &&
                props.SHOOT_ENEMY_NUM < props.THREE_FIRE) {
            fs = new TwoFireStrategy();
        } else if (props.SHOOT_ENEMY_NUM >= props.THREE_FIRE) {
            fs = new ThreeFireStrategy();
        }
        fs.fire(this);
    }

    public void die() {
        hp--;
        if (hp == 0) {
            setLiving(false);
        }

    }

    // 是否添加HP
    public void addHP() {
        if (props.SHOOT_ENEMY_NUM % props.ADD_HP_NUM == 0 && props.SHOOT_ENEMY_NUM != 0 && props.SHOOT_ENEMY_NUM != props.BEFORE_ADD_HP_SHOOT_NUM) {
            props.BEFORE_ADD_HP_SHOOT_NUM = props.SHOOT_ENEMY_NUM;
            // hp增加
            this.hp++;
        }
    }
}